Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgirgen: remove the var expression fixup #1482

Merged

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Dec 25, 2024

Summary

Remove a fixup in cgirgen related to var parameters in cgirgen
and correct the AST construction necessitating it. There's no change in
language semantics.

Details

In typed PNode AST, var parameters always have to be dereferenced
first, including when being passed to other var parameters (resulting
in (HiddenAddr (HiddenDeref x)) trees). Multiple transformations /
places where AST is constructed in the compiler didn't produce AST
adhering to this.

The result was incorrect MIR code being generated (because the location
storing the var parameter was being passed to another parameter,
not the location the var parameter references), though this didn't
cause any trouble so far. cgirgen detected this situation and
adjusted its output accordingly, producing well-formed CGIR AST.

The sources of the problematic PNode AST were:

  • parameter hoisting in semexprs
  • hook lifting in liftdestructors
  • method dispatcher generation in cgmeth

They're changed to produce AST adhering to the aforementioned
requirements, and the fixup in cgirgen is removed.

For the sake of making detection of ill-formed MIR code easier, an
internalAssert is temporarily added to the procedure.
Arguments to `var` parameters weren't hoisted correctly, resulting in
arguments of `var` type that are not `HiddenAddr` expressions.
@zerbina zerbina added compiler/backend Related to backend system of the compiler simplification Removal of the old, unused, unnecessary or un/under-specified language features. labels Dec 25, 2024
@zerbina zerbina added this to the MIR phase milestone Dec 25, 2024
There are two places where hook calls are manually constructed, both
which didn't wrap the first argument in a `nkHiddenAddr` tree.
Method dispatcher creation didn't account for `var` parameters,
placing them in argument positions directly.
It only has a single callsite and is now short and simple enough to no
longer warrant being a procedure. The assertion is also removed again.
@zerbina zerbina changed the title cgirgen: remove var argument fixup cgirgen: remove the var expression fixup Dec 26, 2024
@zerbina zerbina marked this pull request as ready for review December 26, 2024 22:40
Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor suggestions, nothing blocking, and one question.

compiler/sem/semexprs.nim Outdated Show resolved Hide resolved
@@ -3491,10 +3491,22 @@ proc hoistParamsUsedInDefault(c: PContext, call, letSection, defExpr: var PNode)
newNodeI(nkEmpty, letSection.info),
call[paramPos])

call[paramPos] = newSymNode(hoistedVarSym) # Refer the original arg to its hoisted sym
if hoistedVarSym.typ.kind == tyVar:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to consider any skipTypes/unwrapping because this is after generic resolution etc, yes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In part, yes. There's also the assumption throughout parts of the compiler that tyVar cannot appear be nested in a tyAlias or the like.

compiler/sem/liftdestructors.nim Outdated Show resolved Hide resolved
Co-authored-by: Saem Ghani <saemghani+github@gmail.com>
@saem
Copy link
Collaborator

saem commented Dec 27, 2024

/merge

Copy link

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


Notes For Reviewers

A minor prerequisite for the new code generators, which work directly on the MIR and would thus have to perform the fixup themselves.

@chore-runner chore-runner bot added this pull request to the merge queue Dec 27, 2024
Merged via the queue into nim-works:devel with commit ce8ddf6 Dec 27, 2024
37 checks passed
@zerbina zerbina deleted the cgirgen-remove-var-argument-fixup branch December 27, 2024 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/backend Related to backend system of the compiler simplification Removal of the old, unused, unnecessary or un/under-specified language features.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants